﻿using DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication.Models;

namespace WebApplication.Controllers
{
    public class ServerController : Controller
    {
        DataManager dm;
        public ServerController()
        {
            dm = new DataManager();
        }

        public ActionResult Add()
        {
            FillDdl();
            return View();
        }

        [HttpPost] 
        public ActionResult Add(ServerModel server)
        {
            FillDdl();
            if (!ModelState.IsValid)
            {
                ViewData["errors"] = "Объект сервера не валиден.";
                return View();
            }
            DaoFaccade dao = new DaoFaccade();
            dao.AddNewServerObject(server.ToEfEntity());
            return Redirect("/Home/Index");
        }

        [HttpPost]
        public ActionResult GetServerInfo(int idServer)
        {
            return PartialView(dm.ServerManager.GetById(idServer));
        }

        public ActionResult Delete(int id)
        {
            dm.ServerManager.Delete(id);
            dm.SaveChanges();
            return RedirectToRoute(new { controller = "Home", action = "Index" });
        }

        private void FillDdl()
        {
            ViewData["ServerType"] = dm.ServerTypeManager
                 .GetAll()
                 .Select(x => new SelectListItem
                 {
                     Text = x.Name,
                     Value = x.id.ToString()
                 });
        }

        public ActionResult GetServersObjects(int idServer)
        {
            return PartialView(dm.ServerObjectsManager.GetByServerIdNotDeleted(idServer));
        }
    }
}
